/**
* @file main.c
* @brief main entry
* @details application entry
* @author huangbin
* @version V1.0.0
* @date 2025-04-20
* @copyright All documents are copyrighted by Shenzhen Xinlongwei Technology Co., LTD.
*            They may not be used for commercial purposes without permission
*/

#include "inc.h"
#include "arm_2d_helper.h"
#include "arm_2d_scenes.h"
#include "arm_2d_disp_adapters.h"
#include "arm_2d_disp_adapter_0.h"
#include "scene_loder.h"
#include "arm_2d_scene_mainmenu.h"
#include "arm_2d_scene_tjpgd.h"

#if defined(RTE_Acceleration_Arm_2D_Extra_Benchmark)
#   include "arm_2d_benchmark.h"
#endif
#include "arm_2d_scene_rickrolling.h"

extern void HD24151C22_fsmc_mcu_001_init(DRVLCD *pLcd);
extern void HD128006C15_spi4_001_init(DRVLCD *pLcd);
extern void HD096003H13_spi4_001_init(DRVLCD *pLcd);

static void system_init(void);
//工程使用了sct文件，如果使用bootloader运行，需要修改sct文件，同时修改VECT_TAB_OFFSET定义
int main(void)
{

    /* print system info */
    do {
        RCC_ClocksTypeDef clocks;
        
        RCC_ClkConfiguration();
        RCC_GetClocksFreq(&clocks);
        usart1_init(NULL);  //调试串口优先初始化
        
       DebugPrintf("SYSCLK: %3.1fMhz, HCLK: %3.1fMhz, PCLK1: %3.1fMhz, PCLK2: %3.1fMhz\r\n", \
                (float)clocks.SYSCLK_Frequency/1000000, (float)clocks.HCLK_Frequency/1000000, \
                (float)clocks.PCLK1_Frequency/1000000, (float)clocks.PCLK2_Frequency / 1000000);
        DebugPrintf("FW version: %d.%d.%d\r\n",VERSION_MAJOR,VERSION_MINOR,VERSION_MICRO);
        GetFlashCapacity();
        GetChipId(NULL,NULL,NULL);
    } while(0);
    
//    JtagRemap();
    system_init();

    arm_irq_safe {
        arm_2d_init();
    }
    disp_adapter0_init();
    DebugPrintf("arm2d init OK!\r\n"); 
    xmGuiInit();
    DebugPrintf("xmgui init OK!\r\n"); 
#if defined(__PERF_COUNTER_COREMARK__)
    printf("Run Coremark...\r\n");
    coremark_main();
#endif

#if USE_AUTO_LOADER_DEMO == 1  
    
#if defined(RTE_Acceleration_Arm_2D_Extra_Benchmark)
    arm_2d_run_benchmark();
#else
        arm_2d_scene_player_register_before_switching_event_handler(
            &DISP0_ADAPTER,before_scene_switching_handler);
        arm_2d_scene_player_switch_to_next_scene(&DISP0_ADAPTER);
#endif

    while (1) {
        gui_task(&g_tGUI, 60);//lock frame for 60
        load_scene_timeout_check();
        osalRunSystem();
    }
#elif USE_MENU_DEMO == 1
    arm_2d_scene_mainmenu_init(&DISP0_ADAPTER);
    while (1) {
        gui_task(&g_tGUI, 60);//lock frame for 60
        osalRunSystem();
    }
#endif    
}

static
void hardware_init(void)
{
    Timer2Init();
    Timer3Init();
    sFLASH_Init();
    OsalInit();
    KeyInit();
    tp_Init();
}

static
void system_init(void)
{
//    DSP_Start();
    init_cycle_counter(false); //systick留给perf_counter使用，确保在一些短时间关闭中断环境，延时的准确性

    hardware_init();

    lcdRegisterDevice(SCREEN_SUB1,HD24151C22_fsmc_mcu_001_init);
//    lcdRegisterDevice(SCREEN_SUB1,HD128006C15_spi4_001_init);
    lcdBackLightSet(SCREEN_SUB1,LCD_BACKLIGHT_ON);
    DebugPrintf("Hardware init OK!\r\n");    
}


//////////////////////////////File end/////////////////////////////////////

